//
//  ViewController.m
//  WisdomLive
//
//  Created by UserLcc on 2018/5/9.
//  Copyright © 2018年 com.jszhaomi. All rights reserved.
//

#import "LoginViewController.h"
#import "WSHttpManager.h"
#import "Masonry.h"
#import "SVProgressHUD.h"
#import "AppDelegate.h"
#import "NENavigationController.h"
#import "PrepareViewController.h"
@interface LoginViewController ()
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) UITextField *pwdTF;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *bgImage = [UIImageView new];
    [self.view addSubview:bgImage];
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [bgImage setImage:[UIImage imageNamed:@"BG"]];
    
    UIView *topView = [UIView new];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(140*ratio);
        make.centerX.equalTo(self.view);
        make.height.equalTo(@51);
    }];
    
    UIImageView *leftImage = [UIImageView new];
    [topView addSubview:leftImage];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(topView);
        make.width.equalTo(@51);
    }];
    [leftImage setImage:[UIImage imageNamed:@"Logo"]];
    
    UIImageView *rightImage = [UIImageView new];
    [topView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.equalTo(topView);
        make.width.equalTo(@108);
        make.height.equalTo(@46);
        make.left.equalTo(leftImage.mas_right).offset(19);
    }];
    [rightImage setImage:[UIImage imageNamed:@"文字"]];
    
    UIImageView *phoneImage = [UIImageView new];
    [self.view addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(48*ratio);
        make.top.equalTo(topView.mas_bottom).offset(170*ratio);
        make.width.height.equalTo(@24);
    }];
    [phoneImage setImage:[UIImage imageNamed:@"ic_phone"]];
    
    _phoneTF = [UITextField new];
    [self.view addSubview:_phoneTF];
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImage);
        make.left.equalTo(phoneImage.mas_right).offset(12);
        make.right.equalTo(self.view).offset(-48*ratio);
    }];
    _phoneTF.placeholder = @"请输入手机号";
    
    UIView *line1 = [UIView new];
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage);
        make.right.equalTo(_phoneTF);
        make.top.equalTo(_phoneTF.mas_bottom);
        make.height.equalTo(@0.5);
    }];
    line1.backgroundColor = UIColorFromRGB(0xB8BECC);
    
    UIImageView *pwdImage = [UIImageView new];
    [self.view addSubview:pwdImage];
    [pwdImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(48*ratio);
        make.top.equalTo(phoneImage.mas_bottom).offset(30);
        make.width.height.equalTo(@24);
    }];
    [pwdImage setImage:[UIImage imageNamed:@"ic_password"]];
    
    _pwdTF = [UITextField new];
    [self.view addSubview:_pwdTF];
    [_pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImage);
        make.left.equalTo(pwdImage.mas_right).offset(12);
        make.right.equalTo(self.view).offset(-48*ratio);
    }];
    _pwdTF.placeholder = @"请输入密码";
    
    UIView *line2 = [UIView new];
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pwdImage);
        make.right.equalTo(_pwdTF);
        make.top.equalTo(_pwdTF.mas_bottom);
        make.height.equalTo(@0.5);
    }];
    line2.backgroundColor = UIColorFromRGB(0xB8BECC);
    
    UIButton *loginBtn = [UIButton new];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2).offset(40*ratio);
        make.left.right.equalTo(line2);
        make.height.equalTo(@44);
    }];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.layer.masksToBounds = YES;
    loginBtn.backgroundColor = UIColorFromRGB(0x0B9FFF);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)login{
    if(_phoneTF.text.length == 0){
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if(_pwdTF.text.length == 0){
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [SVProgressHUD show];
    [WSHttpManager loginWithMobile:_phoneTF.text
                          password:_pwdTF.text
                          complete:^(DCHttpResult result, NSString *errorMessage, id responseData) {
        if (result) {
            [[NSUserDefaults standardUserDefaults]  setValue:_phoneTF.text forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setValue:_pwdTF.text forKey:@"password"];
            
            PrepareViewController *rootVC = [[PrepareViewController alloc] init];
            NENavigationController* nav = [[NENavigationController alloc]initWithRootViewController:rootVC];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        }
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
